package states
{
	import org.flixel.*;
	import org.flixel.plugin.photonstorm.*;
	
	/**
	 * ...
	 * @author JAC
	 */
	public class OptionState extends FlxState
	{	
		// Selections
		private static const SELECT_MUSIC:uint = 0;
		private static const SELECT_SOUND:uint = 1;
		
		// Bars
		private static var _musicBar:FlxBar;
		private static var _soundBar:FlxBar;
		
		// Items
		private static var _musicButton:FlxSprite;
		private static var _soundButton:FlxSprite;
		private static var _overlayLeft:FlxSprite;
		private static var _overlayBottom:FlxSprite;
		private static var _optionInstructions:FlxText;
		
		// Variables
		private static var _selection:uint;
		
		override public function create():void
		{
			super.create();
			
			add(Registry.epicBackground);
			Registry.epicBackground.setAll("alpha", 0.75);
			
			addSelections();
			
			FlxG.play(Assets.SndBeep, Registry.soundVolume);
		}
		
		override public function destroy():void
		{
			Registry.epicBackground.setAll("alpha", 1.0);
			clear();
			
			super.destroy();
		}
		
		override public function update():void
		{
			super.update();
			
			animateSelections();
			updateSelections();
			updateBars();
			checkInputs();
		}
		
		public static function init():void
		{
			_selection = SELECT_MUSIC;
			
			initSelections();
		}
		
		private static function initSelections():void
		{
			_overlayLeft = new FlxSprite();
			_overlayLeft.makeGraphic(170, FlxG.height - 50, 0xFF000000, true);
			_overlayLeft.alpha = 0.5;
			_overlayLeft.x = 25;
			
			_overlayBottom = new FlxSprite();
			_overlayBottom.makeGraphic(FlxG.width, 50, 0xFF000000, true);
			_overlayBottom.alpha = 0.5;
			_overlayBottom.y = FlxG.height - 50;
			
			_optionInstructions = new FlxText(0, 0, FlxG.width);
			_optionInstructions.font = "cooper_black";
			_optionInstructions.alignment = "center";
			_optionInstructions.size = 20;
			_optionInstructions.color = 0xFFFFFFFF;
			_optionInstructions.text = "UP/DOWN to select. LEFT/RIGHT to change volume.\nSPACE to go back.";
			_optionInstructions.y = FlxG.height - _optionInstructions.height;
			
			_musicButton = new FlxSprite();
			_musicButton.loadGraphic(Assets.ImgMenuMusic, true, false, 100, 36, true);
			_musicButton.addAnimation("default", [0], 1, false);
			_musicButton.addAnimation("blink", [1, 0], 6);
			_musicButton.x = 50;
			_musicButton.y = 150;
			
			_soundButton = new FlxSprite();
			_soundButton.loadGraphic(Assets.ImgMenuSound, true, false, 111, 36, true);
			_soundButton.addAnimation("default", [0], 1, false);
			_soundButton.addAnimation("blink", [1, 0], 6);
			_soundButton.x = 50;
			_soundButton.y = 350;
			
			_musicBar = new FlxBar(225, 150, FlxBar.FILL_LEFT_TO_RIGHT, 475, 35, null, null, 0, 100, false);
			_musicBar.createFilledBar(0xAA000000, 0xAAFFFF00);
			_musicBar.currentValue = Registry.musicVolume * 100;
			
			_soundBar = new FlxBar(225, 350, FlxBar.FILL_LEFT_TO_RIGHT, 475, 35, null, null, 0, 100, false);
			_soundBar.createFilledBar(0xAA000000, 0xAAFFFF00);
			_soundBar.currentValue = Registry.soundVolume * 100;
		}
		
		private function animateSelections():void
		{
			switch (_selection)
			{
				case SELECT_MUSIC: 
				{
					_musicButton.play("blink");
					_soundButton.play("default");
					break;
				}
				case SELECT_SOUND: 
				{
					_musicButton.play("default");
					_soundButton.play("blink");
					break;
				}
				default: 
				{
					break;
				}
			}
		}
		
		private function updateSelections():void
		{
			if (FlxG.keys.justPressed("UP"))
			{
				if (_selection != SELECT_MUSIC)
				{
					_selection--;
					FlxG.play(Assets.SndClick, Registry.soundVolume);
				}
			}
			
			if (FlxG.keys.justPressed("DOWN"))
			{
				if (_selection != SELECT_SOUND)
				{
					_selection++;
					FlxG.play(Assets.SndClick, Registry.soundVolume);
				}
			}
		}
		
		private function updateBars():void
		{
			_musicBar.currentValue = Registry.musicVolume * 100;
			_soundBar.currentValue = Registry.soundVolume * 100;
		}
		
		private function checkInputs():void
		{
			if (FlxG.keys.LEFT)
			{
				if (_selection == SELECT_MUSIC)
				{
					if (Registry.musicVolume > 0)
					{
						Registry.musicVolume = Registry.musicVolume - 0.01;
						FlxG.music.volume = Registry.musicVolume;
					}
				}
				
				if (_selection == SELECT_SOUND)
				{
					if (Registry.soundVolume > 0)
					{
						Registry.soundVolume = Registry.soundVolume - 0.01;
						FlxG.play(Assets.SndBeep, Registry.soundVolume);
					}
				}
			}
			
			if (FlxG.keys.RIGHT)
			{
				if (_selection == SELECT_MUSIC)
				{
					if (Registry.musicVolume < 1)
					{
						Registry.musicVolume = Registry.musicVolume + 0.01;
						FlxG.music.volume = Registry.musicVolume;
					}
				}
				
				if (_selection == SELECT_SOUND)
				{
					if (Registry.soundVolume < 1)
					{
						Registry.soundVolume = Registry.soundVolume + 0.01;
						FlxG.play(Assets.SndBeep, Registry.soundVolume);
					}
				}
			}
			
			if (FlxG.keys.SPACE)
			{
				Registry.saveVolumes();
				FlxG.switchState(new MenuState());
			}
		}
		
		private function addSelections():void
		{
			add(_overlayLeft);
			add(_overlayBottom);
			add(_optionInstructions);
			add(_musicButton);
			add(_soundButton);
			add(_musicBar);
			add(_soundBar);
		}
	}
}